/*
 * SessionsAgent.java
 *
 * Created on 23 wrzesień 2007, 16:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.agents;

import info.mikrotubula.db.Sessions;
import info.mikrotubula.managers.SessionsManagerLocal;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;


/**
 *
 * @author Krystian
 */
public class SessionsAgent {

    @EJB
    private SessionsManagerLocal sessionsManagerBean;
    private String userName;
    private String userPassword;
    private Sessions session;
    private Boolean loggedIn = false;
    private Boolean showLoginBox = false;
    private Boolean pollEnabled = true;
    private Sessions activeSession;
    
    /** Creates a new instance of SessionsAgent */
    public SessionsAgent() {
    }
    
    public void createSession() {
        this.setSession(sessionsManagerBean.create());
        this.loggedIn = false;
        this.showLoginBox = true;
        FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_INFO,"Logowanie: Proszę się zalogować", "Prosze sie zalogowac"));
        
    }
    
    public void login() {
        if (getUserName().isEmpty() || getUserPassword().isEmpty()) {
            FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Logowanie: Proszę wprowadzić nazwę użytkownika i hasło", "Proszę wprowadzić nazwę użytkownika i hasło"));
            return;
        }
        this.session = sessionsManagerBean.authenticate(session, userName, userPassword);
        if (session.isSesAuth()) { 
            this.loggedIn = true;
            this.showLoginBox = false;
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("session",session); 
        } else {
            FacesContext.getCurrentInstance().addMessage("login-box", new FacesMessage(FacesMessage.SEVERITY_INFO,"Logowanie: Wprowadzone dane nie są poprawne", "Wprowadzone dane nie są poprawne"));
        }
    }
    
    public Boolean isLoggedIn() {
        if(activeSession != null && activeSession.isSesAuth()) {
            return new Boolean(true);
        }
        if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("session") != null
                && FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("session") instanceof Sessions
                && ((Sessions) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("session")).isSesAuth()) {
            this.activeSession = (Sessions) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("session");
            return new Boolean(true);
        }
        return new Boolean(false);
    }
    
    public String logout() {
        if(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("session") != null) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("session");
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
        session.invalidate();
        return "logout";
    }
    
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public void closeLoginBox() {
        this.showLoginBox = false;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public Sessions getSession() {
        return session;
    }

    public void setSession(Sessions session) {
        this.session = session;
    }

    public Boolean getLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(Boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    public Boolean getShowLoginBox() {
        return showLoginBox;
    }

    public void setShowLoginBox(Boolean showLoginBox) {
        this.showLoginBox = showLoginBox;
    }

    public Boolean getPollEnabled() {
        return pollEnabled;
    }

    public void setPollEnabled(Boolean pollEnabled) {
        this.pollEnabled = pollEnabled;
    }

    public Sessions getActiveSession() {
        return activeSession;
    }

    public void setActiveSession(Sessions activeSession) {
        this.activeSession = activeSession;
    }
    
}
